package student.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import student.aistar.model.bo.Result;
import student.aistar.model.entity.BoxSeckill;
import student.aistar.model.entity.Product;
import student.aistar.model.entity.User;
import student.aistar.service.IBoxSeckillService;
import student.aistar.service.IProductService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Auther: zhugefeng
 * @Date:2021/10/13 20:55
 **/
@Controller
public class BoxSeckillController {

    @Autowired
    private IBoxSeckillService boxSeckillService;

    @Autowired
    private IProductService productService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/boxseckill")
    public String boxseckill(HttpServletRequest request) {
        List<BoxSeckill> boxSeckillList = boxSeckillService.getAllBoxSeckill();
        List<Product> productList = new ArrayList<>();
        List<Long> seckillTimeList = new ArrayList<>();
        for (int i = 0; i < boxSeckillList.size(); i++) {
            Product product = productService.findById(boxSeckillList.get(i).getProductid());
            productList.add(product);
            seckillTimeList.add(boxSeckillList.get(i).getStarttime().getTime());
        }
        request.setAttribute("boxproduct", productList);
        request.setAttribute("boxSeckillList", boxSeckillList);
        request.setAttribute("seckillTimeList", seckillTimeList);
        return "mi/boxseckill/boxseckill";
    }

    @PostMapping("/seckillBox")
    @ResponseBody
    public Result boxkill(long killId,HttpSession session) {
        //获取秒杀状态
        String status = boxSeckillService.getBoxStatus(killId);

        //秒杀活动尚未开始
        if (status.equals("0")) {

            return new Result("404", "sorry,秒杀活动尚未开始!");
        }
        if (status.equals("2"))
            return new Result("500", "sorry,秒杀活动已经结束!");

        User user = (User) session.getAttribute("user");
        ListOperations lop = redisTemplate.opsForList();

        Object o = lop.leftPop("boxkill_" + killId);
        if (o == null)
            return new Result("400", "sorry,当前商品已售罄");

        SetOperations sets = redisTemplate.opsForSet();

        boolean flag = sets.isMember("seckill_" + killId, user.getUserName());
        if (flag) {
            //此人已抢购过
            lop.leftPush("boxkill" + killId, killId);
            return new Result("400", "您的秒杀名额只有一个!");
        } else {
            sets.add("seckill_" + killId, user.getUserName());
            boxSeckillService.saveBoxOrder(user.getUserName(),killId);
            return new Result("200","恭喜您抢购成功!");
        }

    }
}
